// CpuTemp.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <Windows.h>
#include "OlsApiInit.h"
#include <bitset>
using namespace std;
int GetIntelCpuTemp()
{
	DWORD dw_eax=0,dw_ebx=0,dw_ecx=0,dw_edx=0;
	int iTjunction=0;
	bitset<32> bt_temp;
	bitset<32> bt_temperature;
	if (IsCpuid()&&IsMsr())
	{
		Cpuid(0x0,&dw_eax,&dw_ebx,&dw_ecx,&dw_edx);
		if (dw_eax<6)
		{
			return 0;
		}

		Cpuid(0x6,&dw_eax,&dw_ebx,&dw_ecx,&dw_edx);
		bt_temp=dw_eax;
		if (bt_temp[0]!=1)
		{
			return 0;
		}
		
		Rdmsr(0xee,&dw_eax,&dw_edx);
		bt_temp=dw_eax;
		if (bt_temp[29]==1)
		{
			iTjunction=85;
		}
		else
		{
			iTjunction=100;
		}

		dw_eax=0;
		Rdmsr(0x19c,&dw_eax,&dw_edx);
		bt_temp=dw_eax;
		bt_temperature=0;
		for (int i=15;i<23;i++)
		{
			bt_temperature[i-15]=bt_temp[i];
		}
		return iTjunction-bt_temperature.to_ulong();
	}
	return 0;
}
int GetAmdCpuTemp()
{
	return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
	HMODULE hm;
	if (InitOpenLibSys(&hm))
	{
		printf("%u",GetIntelCpuTemp());
		DeinitOpenLibSys(&hm);
	}
	else
	{
		printf("Init Error!\n");
	}
	return 0;
}

